//
//  HXMineController.m
//  爱鲜蜂
//
//  Created by 洪东恒 on 16/4/14.
//  Copyright © 2016年 建国. All rights reserved.
//

#import "HXMineController.h"
#import "HXTopHeaderView.h"
#import "HXBottonFoodView.h"
#import "HXHomeSettingCollectionViewCell.h"
#import "HDHMeItem.h"

#import "HXSettingTVC.h"
#import "HXStoreCollectVC.h"
#import "HXWaresCollectVC.h"
#import "HXMemberCenter.h"

#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>

@interface HXMineController ()

@property (nonatomic,strong) NSMutableArray *meItem;

@end

@implementation HXMineController

static NSString * const reuseIdentifier = @"Cell";
static NSString * const HeaderViewID = @"HeaderView";
static NSString * const FooterViewID = @"FootView";
//因为是用alloc init 创建的 所以重写init方法 在里面布局
- (instancetype)init {
    //流水布局
    UICollectionViewFlowLayout *flowL = [[UICollectionViewFlowLayout alloc] init];
    //设置每一个格子的大小
    CGFloat itemWidth = (SCREEN_WIDTH - 6) / 4;
    CGFloat itemHeight = 100;
    
    flowL.itemSize = CGSizeMake(itemWidth, itemHeight);
    //设置最小行间距
    flowL.minimumLineSpacing = 10;
    //设置每个格子之间的间距
    flowL.minimumInteritemSpacing = 2;
    
    //设置滚动的方向   垂直滚动
    flowL.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    return [super initWithCollectionViewLayout:flowL];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置UI
    [self setupUI];
    
    //设置headerview
    [self setupHeaderView];
    
    //设置中间的CollectionView
    //设置数据
    [self setupData];
    
    //设置foodview
    [self setupFooterView];

    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HXHomeSettingCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
   
}

//设置UI
-(void)setupUI{
    self.collectionView.backgroundColor = HXGlobeColor;
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_my_settings_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(setupSetting)];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"v2_my_avatar_bg"] forBarMetrics:(UIBarMetricsDefault)];

}

-(void)setupSetting{
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"HXSettingTVC" bundle:nil];
    HXSettingTVC *setting = [story instantiateInitialViewController];
    [self.navigationController pushViewController:setting animated:YES];
    
}



//设置headerview
-(void)setupHeaderView
{

    //注册 header类型
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HXTopHeaderView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewID];

}

//设置foodview
-(void)setupFooterView
{
    //注册 foot类型

    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HXBottonFoodView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterViewID];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//请求数据
-(void)setupData
{
    //导入控件  会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置请求头
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"a"] = @"square";
    dict[@"c"] = @"topic";
    
    //发送请求
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        //把数据写到plist 文件中去
//        [responseObject writeToFile:@"/Users/hongdongheng/Desktop/me.plist" atomically:YES];
        NSArray *array = responseObject[@"square_list"];
        
        
        _meItem = [HDHMeItem mj_objectArrayWithKeyValuesArray:array];
        
        //刷新表格
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"失败了%@",error);
    }];
    //设置数据
}

//会员中心
- (IBAction)clickIcon:(id)sender {
    
    HXMemberCenter *center = [[HXMemberCenter alloc]init];
    [self.navigationController pushViewController:center animated:YES];
    
}
//商品收藏
- (IBAction)waresCollect:(id)sender {
    
    HXWaresCollectVC *wares = [[HXWaresCollectVC alloc]init];
    [self.navigationController pushViewController:wares animated:YES];
    
}
//店铺收藏
- (IBAction)storeCollect:(id)sender {
    
    HXStoreCollectVC *store = [[HXStoreCollectVC alloc]init];
    [self.navigationController pushViewController:store animated:YES];
    
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.meItem.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 
    HXHomeSettingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.item = self.meItem[indexPath.item];

    return cell;
}

#pragma mark <UICollectionViewDelegate>

//这个方法是返回 Header的大小 size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 160);
}

//这个方法是返回 footer的大小 size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 150);
}


//这个也是最重要的方法 获取Headerview  footerview的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
    //从缓存中获取 Headercell
    HXTopHeaderView *Headercell = (HXTopHeaderView  *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewID forIndexPath:indexPath];
        reusableview = Headercell;
    }
    
    if (kind == UICollectionElementKindSectionFooter){
        //从缓存中获取 footercell
    HXBottonFoodView *footercell = (HXBottonFoodView  *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterViewID forIndexPath:indexPath];
        
        footercell.backgroundColor = [UIColor greenColor];
        reusableview = footercell;
    }
    return reusableview;
}

//设定collectionView(指定区)的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //UIEdgeInsets insets = {top, left, bottom, right};
    return UIEdgeInsetsMake(10, 0, 10, 0);
}

@end
